package com.suda.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.suda.dao.VehicleMapper;
import com.suda.pojo.Vehicle;
import com.suda.service.VehicleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/****
 * @Author:zhuyongjie
 * @Description:Vehicle业务层接口实现类
 * @Date 2019/6/14 0:16
 *****/
@Service
public class VehicleServiceImpl implements VehicleService {

    @Autowired
    private VehicleMapper vehicleMapper;


    /**
     * Vehicle条件+分页查询
     * @param vehicle 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<Vehicle> findPage(Vehicle vehicle, int page, int size){
        //分页
        PageHelper.startPage(page,size);
        //搜索条件构建
        Example example = createExample(vehicle);
        //执行搜索
        return new PageInfo<Vehicle>(vehicleMapper.selectByExample(example));
    }

    /**
     * Vehicle分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<Vehicle> findPage(int page, int size){
        //静态分页
        PageHelper.startPage(page,size);
        //分页查询
        return new PageInfo<Vehicle>(vehicleMapper.selectAll());
    }

    /**
     * Vehicle条件查询
     * @param vehicle
     * @return
     */
    @Override
    public List<Vehicle> findList(Vehicle vehicle){
        //构建查询条件
        Example example = createExample(vehicle);
        //根据构建的条件查询数据
        return vehicleMapper.selectByExample(example);
    }


    /**
     * Vehicle构建查询对象
     * @param vehicle
     * @return
     */
    public Example createExample(Vehicle vehicle){
        Example example=new Example(Vehicle.class);
        Example.Criteria criteria = example.createCriteria();
        if(vehicle!=null){
            // 货车id
            if(!StringUtils.isEmpty(vehicle.getVehicleId())){
                    criteria.andEqualTo("vehicleId",vehicle.getVehicleId());
            }
            // 车牌号码
            if(!StringUtils.isEmpty(vehicle.getLicensePlateNumber())){
                    criteria.andEqualTo("licensePlateNumber",vehicle.getLicensePlateNumber());
            }
            // 货车类型
            if(!StringUtils.isEmpty(vehicle.getVehicleType())){
                    criteria.andEqualTo("vehicleType",vehicle.getVehicleType());
            }
            // 货车容量
            if(!StringUtils.isEmpty(vehicle.getCapacity())){
                    criteria.andEqualTo("capacity",vehicle.getCapacity());
            }
            // 货车状态
            if(!StringUtils.isEmpty(vehicle.getVehicleStatus())){
                    criteria.andEqualTo("vehicleStatus",vehicle.getVehicleStatus());
            }
        }
        return example;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(String id){
        vehicleMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改Vehicle
     * @param vehicle
     */
    @Override
    public void update(Vehicle vehicle){
        vehicleMapper.updateByPrimaryKey(vehicle);
    }

    /**
     * 增加Vehicle
     * @param vehicle
     */
    @Override
    public void add(Vehicle vehicle){
        vehicleMapper.insert(vehicle);
    }

    /**
     * 根据ID查询Vehicle
     * @param id
     * @return
     */
    @Override
    public Vehicle findById(String id){
        return  vehicleMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询Vehicle全部数据
     * @return
     */
    @Override
    public List<Vehicle> findAll() {
        return vehicleMapper.selectAll();
    }
}
